//
//  SMACUserMoadel.swift
//  SmiraAiChat
//
//  Created by mumu on 2024/9/23.
//

import UIKit

class SMACUserMoadel: Codable {
    var username :String?
    var avatorname :String?
    var email :String?
    var birthday :String?
    var location :String?
    var abountMe :String?
    var wallet :Int?
    var following :Int?
    var followers :Int?
    var relation:Int?
    var userID :String?
    var trends:Array<SMACTrendMoadel>? //发帖
    enum CodingKeys: String, CodingKey {
        case username
        case avatorname
        case email
        case birthday
        case abountMe
        case location
        case wallet
        case following
        case followers
        case relation
        case userID
        case trends
    }
    required init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        username = try container.decodeIfPresent(String.self, forKey: .username)
        avatorname   = try container.decodeIfPresent(String.self, forKey: .avatorname)
        email   = try container.decodeIfPresent(String.self, forKey: .email)
     
        birthday  = try container.decodeIfPresent(String.self, forKey: .birthday)
       
        abountMe   = try container.decodeIfPresent(String.self, forKey: .abountMe)
        location  = try container.decodeIfPresent(String.self, forKey: .location)
        wallet  = try container.decodeIfPresent(Int.self, forKey: .wallet)
        following  = try container.decodeIfPresent(Int.self, forKey: .following)
        followers  = try container.decodeIfPresent(Int.self, forKey: .followers)

        relation  = try container.decodeIfPresent(Int.self, forKey: .relation)
        userID   = try container.decodeIfPresent(String.self, forKey: .userID)
        trends  = try container.decodeIfPresent(Array<SMACTrendMoadel> .self, forKey: .trends)
    }
    
    init(userDic:[String:Any]) {
        userID =  userDic["relation"] as? String
        username = userDic["Name"] as? String
        avatorname = userDic["avatorname"] as? String
        email =  userDic["avatorname"] as? String
        birthday =  userDic["birthday"] as? String
        abountMe =  userDic["abountMe"] as? String
        location =  userDic["location"] as? String
        wallet =  (userDic["wallet"] as? NSNumber)?.intValue
        following =  (userDic["following"] as? NSNumber)?.intValue
        followers =  (userDic["followers"] as? NSNumber)?.intValue
        relation = (userDic["relation"] as? NSNumber)?.intValue
        
        guard let trends =  userDic["trends"] as? [Dictionary<String,Any>] else {return}
        var list = Array<SMACTrendMoadel>.init()
        
        for (_,trend) in trends.enumerated() {
            let trendmodel = SMACTrendMoadel.init(trendDic: trend)
            list.append(trendmodel)
        }
        
        self.trends = list
    }
}
